import {Command} from '../constant';
import _ from 'lodash';
import {IMainReducer} from '../types';
import {Action} from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  dateTime: '',
  remarks: '',
  list: [
    {
      name: '5-10KG',
      desc: '约8-15件',
    },
    {
      name: '10-20KG',
      desc: '约20-40件',
    },
    {
      name: '20-30KG',
      desc: '约40-60件',
    },
    {
      name: '30KG以上',
      desc: '60件以上',
    },
  ],
  listSelected: 0,
  dateModel: false,
  remarksModel: false,
  successModel: false,
  checked: false,
  daysArr: [],
  daysSelected: 0,
  timeArr: [
    {
      name: '08:00-10:00',
    },
    {
      name: '10:00-12:00',
    },
    {
      name: '12:00-14:00',
    },
    {
      name: '14:00-16:00',
    },
    {
      name: '16:00-18:00',
    },
  ],
  timeSelected: null,
  //地址相关数据
  address: {},
  type: '',
  orderId: '',
  cancelModel: false,
  cancellationOptions: 0, //取消类型
  cancellationReason: '', //取消原因
  orderNo: '',
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const {type, payload} = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, {...payload, key: 'main'});

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}

//create by moon https://github.com/creasy2010/moon
